#include "HelloWorldScene.h"
#include "md5.h"
#include "jtptest.h"
//**********************JniHelper start
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>

#if 1
#define  LOG_TAG    "JniHelper"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define  LOGD(...)
#endif
//**********************JniHelper end
#endif
#include "extensions/cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

Scene* HelloWorld::createScene() {
	Scene *scene = Scene::create();
	HelloWorld *layer = HelloWorld::create();
	scene->addChild(layer);
	return scene;
}

bool HelloWorld::init() {
	if (!Layer::init()) {
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	auto closeItem = MenuItemImage::create("CloseNormal.png",
			"CloseSelected.png",
			CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	closeItem->setPosition(
			Vec2(
					origin.x + visibleSize.width
							- closeItem->getContentSize().width / 2,
					origin.y + closeItem->getContentSize().height / 2));

	// create menu, it's an autorelease object
	auto menu = Menu::create(closeItem, NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);

	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label

	auto label = Label::createWithTTF("JTPAY WALLET", "fonts/Marker Felt.ttf",
			24);

	// position the label on the center of the screen
	label->setPosition(
			Vec2(origin.x + visibleSize.width / 2,
					origin.y + visibleSize.height
							- label->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(label, 1);


	/**
	*   create button   left view Start
	*   parameter Organization url do in client
	*/
    auto btn = cocos2d::ui::Button::create();
    btn->loadTextureNormal("btnNormal.png");
    btn->loadTexturePressed("btnHighlighted.png");
    btn->setScale9Enabled(true);   //open scale9
    btn->setTitleText("WAP");     //set title
    btn->setContentSize(Size(100, 20));     //set button size，request setScale9Enabled(true)
    btn->setTag(0);
    btn->setTouchEnabled(true);
    btn->setPosition(Vec2(origin.x + visibleSize.width / 4,
                    origin.y + visibleSize.height / 2 ));
    btn->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownWap,this));

	auto btn1 = cocos2d::ui::Button::create();
    btn1->loadTextureNormal("btnNormal.png");
    btn1->loadTexturePressed("btnHighlighted.png");
    btn1->setScale9Enabled(true);
    btn1->setTitleText("SDK");
    btn1->setContentSize(Size(100, 20));
    btn1->setTag(1);
    btn1->setTouchEnabled(true);
    btn1->setPosition(Vec2(origin.x + visibleSize.width / 4,
                    origin.y + visibleSize.height / 2  + 30));
    btn1->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownSdk,this));
	this->addChild(btn);
	this->addChild(btn1);

	auto btn2 = cocos2d::ui::Button::create();
    btn2->loadTextureNormal("btnNormal.png");
    btn2->loadTexturePressed("btnHighlighted.png");
    btn2->setScale9Enabled(true);
    btn2->setTitleText("WAPOneStep");
    btn2->setContentSize(Size(100, 20));
    btn2->setTag(2);
    btn2->setTouchEnabled(true);
    btn2->setPosition(Vec2(origin.x + visibleSize.width / 4,
                               origin.y + visibleSize.height / 2  - 30));
    btn2->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownWapOneStep,this));
    this->addChild(btn2);

    auto btn3 = cocos2d::ui::Button::create();
    btn3->loadTextureNormal("btnNormal.png");
    btn3->loadTexturePressed("btnHighlighted.png");
    btn3->setScale9Enabled(true);
    btn3->setTitleText("SDKOneStep");
    btn3->setContentSize(Size(100, 20));
    btn3->setTag(3);
    btn3->setTouchEnabled(true);
    btn3->setPosition(Vec2(origin.x + visibleSize.width / 4,
                  origin.y + visibleSize.height / 2  -60));
    btn3->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownSdkOneStep,this));
    this->addChild(btn3);
    labelSwitch = LabelTTF::create("微信", "Marker Felt", 12);
	labelSwitch->setPosition(Vec2(origin.x + visibleSize.width/6,
	        origin.y + visibleSize.height - label->getContentSize().height * 1.5
	        - labelSwitch->getContentSize().height));
	this->addChild(labelSwitch);
	auto btn4 = cocos2d::ui::Button::create();
    btn4->loadTextureNormal("btnNormal.png");
    btn4->loadTexturePressed("btnHighlighted.png");
    btn4->setScale9Enabled(true);
    btn4->setTouchEnabled(true);
    btn4->setTitleText("一步支付方式切换");
	btn4->setContentSize(Size(100, 60));
	btn4->setTag(4);
	btn4->setPosition(Vec2(origin.x + visibleSize.width/3,
                origin.y + visibleSize.height - label->getContentSize().height * 1.5
                - labelSwitch->getContentSize().height));
    btn4->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownSwitch,this));
	this->addChild(btn4);

	/**
    *   create button   right view Start
    *   parameter Organization url do in server
    *   here just need url
    */
    labelPayBrower = LabelTTF::create("浏览器", "Marker Felt", 12);
    labelPayBrower->setPosition(Vec2(origin.x + visibleSize.width / 2 + 30,
            origin.y + visibleSize.height - label->getContentSize().height * 1.5
            - labelPayBrower->getContentSize().height));
    this->addChild(labelPayBrower);
    auto btn41 = cocos2d::ui::Button::create();
    btn41->loadTextureNormal("btnNormal.png");
    btn41->loadTexturePressed("btnHighlighted.png");
    btn41->setScale9Enabled(true);
    btn41->setTouchEnabled(true);
    btn41->setTitleText("网页调起方式");
    btn41->setContentSize(Size(100, 60));
    btn41->setTag(4);
    btn41->setPosition(Vec2(origin.x + visibleSize.width  * 2 / 3 + 30,
                origin.y + visibleSize.height - label->getContentSize().height * 1.5
                - labelPayBrower->getContentSize().height));
    btn41->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownBrower,this));
    this->addChild(btn41);

    auto btn01 = cocos2d::ui::Button::create();
    btn01->loadTextureNormal("btnNormal.png");
    btn01->loadTexturePressed("btnHighlighted.png");
    btn01->setScale9Enabled(true);
    btn01->setTitleText("WAPURL");
    btn01->setContentSize(Size(100, 20));
    btn01->setTag(01);
    btn01->setTouchEnabled(true);
    btn01->setPosition(Vec2(origin.x + visibleSize.width * 2 / 3,
                    origin.y + visibleSize.height / 2 ));
    btn01->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownWapUrl,this));
    this->addChild(btn01);

	auto btn11 = cocos2d::ui::Button::create();
    btn11->loadTextureNormal("btnNormal.png");
    btn11->loadTexturePressed("btnHighlighted.png");
    btn11->setScale9Enabled(true);
    btn11->setTitleText("SDKURL");
    btn11->setContentSize(Size(100, 20));
    btn11->setTag(11);
    btn11->setTouchEnabled(true);
    btn11->setPosition(Vec2(origin.x + visibleSize.width * 2 / 3,
                    origin.y + visibleSize.height / 2  + 30));
    btn11->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownSdkUrl,this));
	this->addChild(btn11);


	auto btn21 = cocos2d::ui::Button::create();
    btn21->loadTextureNormal("btnNormal.png");
    btn21->loadTexturePressed("btnHighlighted.png");
    btn21->setScale9Enabled(true);
    btn21->setTitleText("WAPOneStepURL");
    btn21->setContentSize(Size(100, 20));
    btn21->setTag(21);
    btn21->setTouchEnabled(true);
    btn21->setPosition(Vec2(origin.x + visibleSize.width * 2 / 3,
                               origin.y + visibleSize.height / 2  - 30));
    btn21->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownWapOneStepUrl,this));
    this->addChild(btn21);

    auto btn31 = cocos2d::ui::Button::create();
    btn31->loadTextureNormal("btnNormal.png");
    btn31->loadTexturePressed("btnHighlighted.png");
    btn31->setScale9Enabled(true);
    btn31->setTitleText("SDKOneStepURL");
    btn31->setContentSize(Size(100, 20));
    btn31->setTag(31);
    btn31->setTouchEnabled(true);
    btn31->setPosition(Vec2(origin.x + visibleSize.width * 2 / 3,
                  origin.y + visibleSize.height / 2  -60));
    btn31->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownSdkOneStepUrl,this));
    this->addChild(btn31);

   	/**
   	*   create button   bottom view Start
   	*   tune up the browser do something
   	**/
    /*auto btnbrower = cocos2d::ui::Button::create();
    btnbrower->loadTextureNormal("btnNormal.png");
    btnbrower->loadTexturePressed("btnHighlighted.png");
    btnbrower->setScale9Enabled(true);
    btnbrower->setTitleText("payBrower");
    btnbrower->setContentSize(Size(100, 20));
    btnbrower->setTag(5);
    btnbrower->setTouchEnabled(true);
    btnbrower->setPosition(Vec2(origin.x + visibleSize.width / 2,
                  origin.y + visibleSize.height / 2  -90));
    btnbrower->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchDownBrower,this));
    this->addChild(btnbrower);*/

	return true;
}
/*
*   It is recommended to use this approach
*
*/
void HelloWorld::touchDownWapUrl(Ref* pSender,cocos2d::ui::Widget::TouchEventType event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
          }
          bool b;
          if(labelPayBrower->getString()=="浏览器"){
             b = JniHelper::getMethodInfo(minfo,
             "org.cocos2dx.cpp.AppActivity",
             "payBrower",
             "(Ljava/lang/String;)V");
          }else{
             b = JniHelper::getMethodInfo(minfo,
             "org.cocos2dx.cpp.AppActivity",
             "WAPPAY",
             "(Ljava/lang/String;)V");
          }
           /*bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",
                                            "WAPPAY",
                                            "(Ljava/lang/String;)V");*/
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jTestUrl =minfo.env->NewStringUTF(TestUrl2.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jTestUrl);
           }
       }
#endif
}
void HelloWorld::touchDownSdkUrl(Ref* pSender,cocos2d::ui::Widget::TouchEventType event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
           }
           bool b;
           if(labelPayBrower->getString()=="浏览器"){
               b = JniHelper::getMethodInfo(minfo,
               "org.cocos2dx.cpp.AppActivity",
               "payBrower",
               "(Ljava/lang/String;)V");
           }else{
               b = JniHelper::getMethodInfo(minfo,
               "org.cocos2dx.cpp.AppActivity",
               "SDKPAY",
               "(Ljava/lang/String;)V");
           }
          /*bool b = JniHelper::getMethodInfo(minfo,
                                           "org.cocos2dx.cpp.AppActivity",
                                           "SDKPAY",
                                           "(Ljava/lang/String;)V");*/
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jTestUrl =minfo.env->NewStringUTF(TestUrl3.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jTestUrl);
           }
       }
#endif
}
void HelloWorld::touchDownWapOneStepUrl(Ref* pSender,cocos2d::ui::Widget::TouchEventType event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
             }

            bool b;
            if(labelPayBrower->getString()=="浏览器"){
                b = JniHelper::getMethodInfo(minfo,
                "org.cocos2dx.cpp.AppActivity",
                "payBrower",
                "(Ljava/lang/String;)V");
            }else{
                b = JniHelper::getMethodInfo(minfo,
                "org.cocos2dx.cpp.AppActivity",
                "WAPPAY",
                "(Ljava/lang/String;)V");
            }
            /*bool b = JniHelper::getMethodInfo(minfo,
                                      "org.cocos2dx.cpp.AppActivity",
                                      "WAPPAY",
                                      "(Ljava/lang/String;)V");*/
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jTestUrl =minfo.env->NewStringUTF(TestUrl1.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jTestUrl);
           }
       }
#endif
}
void HelloWorld::touchDownSdkOneStepUrl(Ref* pSender,cocos2d::ui::Widget::TouchEventType event){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
            JniMethodInfo minfo;
            jobject mActivity;
            bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
            if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
            }
            bool b;
            if(labelPayBrower->getString()=="浏览器"){
                b = JniHelper::getMethodInfo(minfo,
                "org.cocos2dx.cpp.AppActivity",
                "payBrower",
                "(Ljava/lang/String;)V");
            }else{
                b = JniHelper::getMethodInfo(minfo,
                "org.cocos2dx.cpp.AppActivity",
                "SDKPAY",
                "(Ljava/lang/String;)V");
            }
           /*bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",
                                            "SDKPAY",
                                            "(Ljava/lang/String;)V");*/
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jTestUrl =minfo.env->NewStringUTF(TestUrl4.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jTestUrl);
           }
       }
#endif
}
//touchDownWap
/**
 * @ appid The unique identifier of the app。
 * pN_* See the 26 parameters of the access document
 **/
void HelloWorld::touchDownWap(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
            JniMethodInfo minfo;
            jobject mActivity;
            bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                         "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
            if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
           }
           //todo not recommended for comKey stored on the client
           std::string compkey = StringUtils::format("%s", "40F4A509B7459D667B68C9A51A4E14DA");

           std::string p1_usercode = StringUtils::format("%s", "10208331");
           std::string p2_order = HelloWorld::getSystemTime();
           std::string p3_money = StringUtils::format("%s", "1.00");
           std::string p4_returnurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestReturn");
           std::string p5_notifyurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestNotice");
           std::string p6_ordertime = p2_order;
           std::string p9_paymethod = StringUtils::format("%s", "wap");

           std::string p14_customname = StringUtils::format("%s", "name");
           std::string p17_customip = StringUtils::format("%s", "ip");
           std::string p18_product = StringUtils::format("%s", "testName");
           std::string p25_terminal = StringUtils::format("%s", "3");//
           std::string p26_iswappay = StringUtils::format("%s", "5");//
            /**
            * It is highly recommended that the p7_sign data signature process be completed on the server side,
            * very not recommended for merchant payment keys stored on the client
            * demo as a result of the show, in the mobile terminal
            **/
           std::string md5str = "";
           md5str.append(p1_usercode)
                  .append("&")
                  .append(p2_order)
                  .append("&")
                  .append(p3_money)
                  .append("&")
                  .append(p4_returnurl)
                  .append("&")
                  .append(p5_notifyurl)
                  .append("&")
                  .append(p6_ordertime)
                  .append(compkey);

           CCLog("JniHelper::md5str.data()...");
           std::string p7_sign = md5(md5str);

           std::string buffer = "";
           buffer.append(BaseApi);
           buffer.append("?p1_usercode=");
           buffer.append(p1_usercode);
           buffer.append("&p2_order=");
           buffer.append(p2_order);
           buffer.append("&p3_money=");
           buffer.append(p3_money);
           buffer.append("&p4_returnurl=");
           buffer.append(p4_returnurl);
           buffer.append("&p5_notifyurl=");
           buffer.append(p5_notifyurl);
           buffer.append("&p6_ordertime=");
           buffer.append(p6_ordertime);
           buffer.append("&p7_sign=");
           buffer.append(p7_sign);
           buffer.append("&p9_paymethod=");
           buffer.append(p9_paymethod);
           buffer.append("&p14_customname=");
           buffer.append(p14_customname);
           buffer.append("&p17_customip=");
           buffer.append(p17_customip);
           buffer.append("&p18_product=");
           buffer.append(p18_product);
           buffer.append("&p25_terminal=");
           buffer.append(p25_terminal);
           buffer.append("&p26_iswappay=");
           buffer.append(p26_iswappay);
           /*
           * Above for the need to wear the parameters, the following comment out the part of the
           * optional parameters can be added according to demand.
           * Optional parameter definitions Please refer to the documentation for 26 access parameter
           * descriptions
           */
           /*buffer.append("&p8_signtype=");
           buffer.append(p8_signtype);//user define
           buffer.append("&p10_paychannelnum=");
           buffer.append(p10_paychannelnum);//user define
           buffer.append("&p11_cardtype=");
           buffer.append(p11_cardtype);//user define
           buffer.append("&p12_channel=");
           buffer.append(p12_channel);//user define
           buffer.append("&p13_orderfailertime=");
           buffer.append(p13_orderfailertime);//user define
           buffer.append("&p15_customcontacttype=");
           buffer.append(p15_customcontacttype);//user define
           buffer.append("&p16_customcontact=");
           buffer.append(p16_customcontact);//user define
           buffer.append("&p19_productcat=");
           buffer.append(p19_productcat);//user define
           buffer.append("&p20_productnum=");
           buffer.append(p20_productnum);//user define
           buffer.append("&p21_pdesc=");
           buffer.append(p21_pdesc);//user define
           buffer.append("&p22_version=");
           buffer.append(p22_version);//user define
           buffer.append("&p23_charset=");
           buffer.append(p23_charset);//user define
           buffer.append("&p24_remark=");
           buffer.append(p24_remark);//user define*/

           bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",// Point to activity
                                            "WAPPAY",//Point to method
                                            "(Ljava/lang/String;)V");//(string)void no return
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  //java code pass the value,need to convert format
                  jstring jbuffer =minfo.env->NewStringUTF(buffer.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jbuffer);
           }
        }
#endif
}
//touchDownWapOneStep
 /**
  * @ appid The unique identifier of the app。
  * pN_* See the 26 parameters of the access document
  **/
void HelloWorld::touchDownWapOneStep(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
           "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
           }
           //todo not recommended for comKey stored on the client
           std::string compkey = StringUtils::format("%s", "40F4A509B7459D667B68C9A51A4E14DA");

           std::string p1_usercode = StringUtils::format("%s", "10208331");
           std::string p2_order = HelloWorld::getSystemTime();
           std::string p3_money = StringUtils::format("%s", "1.00");
           std::string p4_returnurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestReturn");
           std::string p5_notifyurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestNotice");
           std::string p6_ordertime = p2_order;
           std::string p9_paymethod = payTypeValue;//3 微信 4 支付宝
           std::string p14_customname = StringUtils::format("%s", "name");
           std::string p17_customip = StringUtils::format("%s", "ip");
           std::string p18_product = StringUtils::format("%s", "testName");
           std::string p25_terminal = StringUtils::format("%s", "3");
           std::string p26_iswappay = StringUtils::format("%s", "5");
            /**
            * It is highly recommended that the p7_sign data signature process be completed on the server side,
            * very not recommended for merchant payment keys stored on the client
            * demo as a result of the show, in the mobile terminal
            **/
           std::string md5str = "";
           md5str.append(p1_usercode)
                  .append("&")
                  .append(p2_order)
                  .append("&")
                  .append(p3_money)
                  .append("&")
                  .append(p4_returnurl)
                  .append("&")
                  .append(p5_notifyurl)
                  .append("&")
                  .append(p6_ordertime)
                  .append(compkey);

           CCLog("JniHelper::md5str.data()...");
           std::string p7_sign = md5(md5str);

           std::string buffer = "";
           buffer.append(BaseApi);
           buffer.append("?p1_usercode=");
           buffer.append(p1_usercode);
           buffer.append("&p2_order=");
           buffer.append(p2_order);
           buffer.append("&p3_money=");
           buffer.append(p3_money);
           buffer.append("&p4_returnurl=");
           buffer.append(p4_returnurl);
           buffer.append("&p5_notifyurl=");
           buffer.append(p5_notifyurl);
           buffer.append("&p6_ordertime=");
           buffer.append(p6_ordertime);
           buffer.append("&p7_sign=");
           buffer.append(p7_sign);
           buffer.append("&p9_paymethod=");
           buffer.append(p9_paymethod);
           buffer.append("&p14_customname=");
           buffer.append(p14_customname);
           buffer.append("&p17_customip=");
           buffer.append(p17_customip);
           buffer.append("&p18_product=");
           buffer.append(p18_product);
           buffer.append("&p25_terminal=");
           buffer.append(p25_terminal);
           buffer.append("&p26_iswappay=");
           buffer.append(p26_iswappay);
           /*
           * Above for the need to wear the parameters, the following comment out the part of the
           * optional parameters can be added according to demand.
           * Optional parameter definitions Please refer to the documentation for 26 access parameter
           * descriptions
           */
           /*buffer.append("&p8_signtype=");
           buffer.append(p8_signtype);//user define
           buffer.append("&p10_paychannelnum=");
           buffer.append(p10_paychannelnum);//user define
           buffer.append("&p11_cardtype=");
           buffer.append(p11_cardtype);//user define
           buffer.append("&p12_channel=");
           buffer.append(p12_channel);//user define
           buffer.append("&p13_orderfailertime=");
           buffer.append(p13_orderfailertime);//user define
           buffer.append("&p15_customcontacttype=");
           buffer.append(p15_customcontacttype);//user define
           buffer.append("&p16_customcontact=");
           buffer.append(p16_customcontact);//user define
           buffer.append("&p19_productcat=");
           buffer.append(p19_productcat);//user define
           buffer.append("&p20_productnum=");
           buffer.append(p20_productnum);//user define
           buffer.append("&p21_pdesc=");
           buffer.append(p21_pdesc);//user define
           buffer.append("&p22_version=");
           buffer.append(p22_version);//user define
           buffer.append("&p23_charset=");
           buffer.append(p23_charset);//user define
           buffer.append("&p24_remark=");
           buffer.append(p24_remark);//user define*/

           bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity", 
                                            "WAPPAY", 
                                            "(Ljava/lang/String;)V");
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                //java code pass the value,need to convert format
                  jstring jbuffer =minfo.env->NewStringUTF(buffer.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jbuffer);
           }
        }
#endif
}

//touchDownSdk
/**
  * @ appid The unique identifier of the app。
  * pN_* See the 26 parameters of the access document
  **/
void HelloWorld::touchDownSdk(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
            }
            //todo not recommended for comKey stored on the client
           std::string compkey = StringUtils::format("%s", "40F4A509B7459D667B68C9A51A4E14DA");

           std::string p1_usercode = StringUtils::format("%s", "10208331");
           std::string p2_order = HelloWorld::getSystemTime();
           std::string p3_money = StringUtils::format("%s", "1.00");
           std::string p4_returnurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestReturn");
           std::string p5_notifyurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestNotice");
           std::string p6_ordertime = p2_order;
            /**
            * It is highly recommended that the p7_sign data signature process be completed on the server side,
            * very not recommended for merchant payment keys stored on the client
            * demo as a result of the show, in the mobile terminal
            **/
           std::string md5str = "";
           md5str.append(p1_usercode)
                  .append("&")
                  .append(p2_order)
                  .append("&")
                  .append(p3_money)
                  .append("&")
                  .append(p4_returnurl)
                  .append("&")
                  .append(p5_notifyurl)
                  .append("&")
                  .append(p6_ordertime)
                  .append(compkey);

           CCLog("JniHelper::md5str.data()...");
           std::string p7_sign = md5(md5str);
           std::string p18_product = StringUtils::format("%s", "testName");
           std::string appid = StringUtils::format("%s", "20161111102907658374");

           std::string buffer = "";
           buffer.append(BaseApi);
           buffer.append("?p1_usercode=");
           buffer.append(p1_usercode);
           buffer.append("&p2_order=");
           buffer.append(p2_order);
           buffer.append("&p3_money=");
           buffer.append(p3_money);
           buffer.append("&p4_returnurl=");
           buffer.append(p4_returnurl);
           buffer.append("&p5_notifyurl=");
           buffer.append(p5_notifyurl);
           buffer.append("&p6_ordertime=");
           buffer.append(p6_ordertime);
           buffer.append("&p7_sign=");
           buffer.append(p7_sign);
           buffer.append("&p9_paymethod=");
           buffer.append("sdk");
           buffer.append("&p18_product=");
           buffer.append(p18_product);
           buffer.append("&ext_appid=");
           buffer.append(appid);
           bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",
                                            "SDKPAY",
                                            "(Ljava/lang/String;)V");
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jbuffer =minfo.env->NewStringUTF(buffer.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jbuffer);
           }
       }
#endif
}
//touchDownSdkOneStep
void HelloWorld::touchDownSdkOneStep(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
          }
           //todo not recommended for comKey stored on the client
           std::string compkey = StringUtils::format("%s", "40F4A509B7459D667B68C9A51A4E14DA");

           std::string p1_usercode = StringUtils::format("%s", "10208331");
           std::string p2_order = HelloWorld::getSystemTime();
           std::string p3_money = StringUtils::format("%s", "1.00");
           std::string p4_returnurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestReturn");
           std::string p5_notifyurl = StringUtils::format("%s", "http://pay.jtpay.com/Form/TestNotice");
           std::string p6_ordertime = p2_order;
            /**
            * It is highly recommended that the p7_sign data signature process be completed on the server side,
            * very not recommended for merchant payment keys stored on the client
            * demo as a result of the show, in the mobile terminal
            **/
           std::string md5str = "";
           md5str.append(p1_usercode)
                         .append("&")
                         .append(p2_order)
                         .append("&")
                         .append(p3_money)
                         .append("&")
                         .append(p4_returnurl)
                         .append("&")
                         .append(p5_notifyurl)
                         .append("&")
                         .append(p6_ordertime)
                         .append(compkey);
           CCLog("JniHelper::md5str.data()...");
           std::string p7_sign = md5(md5str);
           std::string p18_product = StringUtils::format("%s", "testName");
           std::string appid = StringUtils::format("%s", "20161111102907658374");

           std::string buffer = "";
           buffer.append(BaseApi);
           buffer.append("?p1_usercode=");
           buffer.append(p1_usercode);
           buffer.append("&p2_order=");
           buffer.append(p2_order);
           buffer.append("&p3_money=");
           buffer.append(p3_money);
           buffer.append("&p4_returnurl=");
           buffer.append(p4_returnurl);
           buffer.append("&p5_notifyurl=");
           buffer.append(p5_notifyurl);
           buffer.append("&p6_ordertime=");
           buffer.append(p6_ordertime);
           buffer.append("&p7_sign=");
           buffer.append(p7_sign);
           buffer.append("&p9_paymethod=");
           buffer.append("sdk");
           buffer.append("&p18_product=");
           buffer.append(p18_product);
           buffer.append("&ext_appid=");
           buffer.append(appid);
           buffer.append("&ext_paytype=");
           buffer.append(payTypeValue);
           bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",
                                            "SDKPAY",
                                            "(Ljava/lang/String;)V");
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jbuffer =minfo.env->NewStringUTF(buffer.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jbuffer);
           }
       }
#endif
}
void HelloWorld::touchDownBrower(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
      /* if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           JniMethodInfo minfo;
           jobject mActivity;
           bool isHave = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity",
                        "rtnActivity","(Ljava/lang/String;)Ljava/lang/Object;");
           if (isHave) {
                jstring jDownLaodAddress =minfo.env->NewStringUTF(DownLaodAddress.c_str());
                mActivity = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jDownLaodAddress);
          }
           bool b = JniHelper::getMethodInfo(minfo,
                                            "org.cocos2dx.cpp.AppActivity",
                                            "payBrower",
                                            "(Ljava/lang/String;)V");
           if (!b) {
                  CCLog("JniHelper::getMethodInfo error...");
           } else {
                  jstring jTestUrl =minfo.env->NewStringUTF(TestUrl.c_str());
                  minfo.env->CallVoidMethod(mActivity, minfo.methodID,jTestUrl);
           }
       }*/
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
          if(labelPayBrower->getString()=="浏览器"){
             labelPayBrower->setString("非浏览器");
          }else{
             labelPayBrower->setString("浏览器");
          }
      }
#endif
}

//touchDownSwitch 
void HelloWorld::touchDownSwitch(Ref* pSender, cocos2d::ui::Widget::TouchEventType event) {
       if(event== cocos2d::ui::Widget::TouchEventType::ENDED){
           if(labelSwitch->getString()=="微信"){
              labelSwitch->setString("支付宝");
              payTypeValue=StringUtils::format("%s", "4");
           }else{
              labelSwitch->setString("微信");
              payTypeValue=StringUtils::format("%s", "3");
           }
       }
}
/* sdk  getpaystatus*/
extern "C"
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
void Java_org_cocos2dx_cpp_AppActivity_setAidlPayStatus(JNIEnv *env, jobject /* this */,
jstring payStatus)
{
    const char *paytus = env->GetStringUTFChars(payStatus, NULL);
    setPayStatus(paytus);
}
#endif
}
std::string HelloWorld::getSystemTime()
{
	struct tm *tm;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	//win32
	time_t timep;
	time(&timep);
	tm = localtime(&timep);
#else
	//android、ios

	struct timeval now;
	gettimeofday(&now, NULL);
	tm = localtime(&now.tv_sec);
#endif
	int year = tm->tm_year + 1900;
	int month = tm->tm_mon + 1;
	int day = tm->tm_mday;
	int hour = tm->tm_hour;
	int min = tm->tm_min;
	int sec = tm->tm_sec;
	char months[3], days[3], hours[3], mins[3], secs[3];
	if (month < 10) {
		sprintf(months, "%s%d", "0", month);
	}
	else {
		sprintf(months, "%d", month);
	}
	if (day < 10) {
		sprintf(days, "%s%d", "0", day);
	}
	else {
		sprintf(days, "%d", day);
	}
	if (hour < 10) {
		sprintf(hours, "%s%d", "0", hour);
	}
	else {
		sprintf(hours, "%d", hour);
	}
	if (min < 10) {
		sprintf(mins, "%s%d", "0", min);
	}
	else {
		sprintf(mins, "%d", min);
	}
	if (sec < 10) {
		sprintf(secs, "%s%d", "0", sec);
	}
	else {
		sprintf(secs, "%d", sec);
	}
	std::string string = StringUtils::format("%d%s%s%s%s%s", year, months, days, hours, mins, secs);
	return string;

}

void HelloWorld::menuCloseCallback(Ref* pSender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
	return;
#endif

	Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}
